package com.example.springboot.service;

import com.example.springboot.entity.Device;
import com.example.springboot.mapper.DeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    public int save(Device device){
        if(device.getId()==null){
            return deviceMapper.insert(device);
        }else{
            return deviceMapper.update(device);
        }
    }

    //批量增加
    public Integer saveBatch(List<Device> devices){
        Integer num = 0;
        for(int i=0; i<devices.size();i++){
            Device device = devices.get(i);
            if(device.getId()==null){
                num += deviceMapper.insert(device);
            }else{
                num += deviceMapper.update(device);
            }
        }
        return num;
    }
}
